//
//  Header.m
//  Product_A
//
//  Created by lanou on 16/5/11.
//  Copyright © 2016年 黄维荣. All rights reserved.
//

#import "Header.h"
#import <UIImageView+WebCache.h>
@implementation Header

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/


- (instancetype)initWithFrame:(CGRect)frame createScrollViewWithPicArray:(NSArray *)picArray{
    self = [super initWithFrame:frame];
    if (self) {
        self.count = picArray.count;
        
        self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, kWidth, 172)];
        _scrollView.contentSize = CGSizeMake(kWidth * (picArray.count + 2), kHeight / 4);
        _scrollView.contentOffset = CGPointMake(kWidth, 0);
        _scrollView.delegate = self;
        _scrollView.bounces = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.pagingEnabled = YES;
        
        self.firstImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kWidth, 172)];
        [_firstImageView sd_setImageWithURL:[NSURL URLWithString:(NSString *)picArray[1]]];
        [_scrollView addSubview:_firstImageView];
        
        self.lastImageView = [[UIImageView alloc]initWithFrame:CGRectMake(kWidth * (picArray.count + 1), 0, kWidth, kHeight / 4)];
        [_lastImageView sd_setImageWithURL:[NSURL URLWithString:(NSString *)[picArray firstObject]]];
        [_scrollView addSubview:_lastImageView];
        
        for (int i = 0; i < picArray.count; i++) {
            self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake(kWidth * (i + 1), 0, kWidth, kHeight / 4)];
            [_imageView sd_setImageWithURL:[NSURL URLWithString:(NSString *)picArray[i]]];
            [_scrollView addSubview:_imageView];
        }
        [self addSubview:_scrollView];
        
        self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(change) userInfo:nil repeats:YES];
        
        self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(0, kHeight / 4 - 20, kWidth, 20)];
        _page.numberOfPages = picArray.count;
        _page.currentPage = 0;
        [self addSubview:_page];
        [_page addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
    }
    return self;
}

- (void)change{
    CGPoint currentOffset = _scrollView.contentOffset;
    currentOffset.x += kWidth;
    [_scrollView setContentOffset:currentOffset animated:YES];
}

- (void)pageAction:(UIPageControl *)page{
    [self.scrollView setContentOffset:CGPointMake((page.currentPage + 1) * kWidth, 0) animated:YES];
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSInteger index = scrollView.contentOffset.x / kWidth;
    self.page.currentPage = index - 1;
    if (index == self.count + 1) {
        [scrollView setContentOffset:CGPointMake(kWidth * 1, 0) animated:NO];
        self.page.currentPage = 0;
    } else if (index == 0){
        [scrollView setContentOffset:CGPointMake(kWidth * self.count, 0) animated:NO];
        self.page.currentPage = self.count - 1;
    }
    
}

@end
